package org.jeese.common.utils;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.jeese.common.shiro.ShiroAuthorizingRealm;
import org.jeese.modules.sys.entity.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description:Shiro工具类
 * @Package:org.jeese.utils
 * @author:wubc
 * @version:1.0
 * @date:2017年10月31日-下午3:26:23
 * @Copyright:2017 Inc. All rights reserved.
 */
public class ShiroUtils {
	private static Logger logger = LoggerFactory.getLogger(ShiroUtils.class);

	public static Session getSession() {
		return SecurityUtils.getSubject().getSession();
	}

	public static Subject getSubject() {
		return SecurityUtils.getSubject();
	}

	public static SysUser getUser() {
		return (SysUser) SecurityUtils.getSubject().getPrincipal();
	}

	public static Long getUserId() {
		return getUser().getUserId();
	}

	public static void setSessionAttribute(Object key, Object value) {
		getSession().setAttribute(key, value);
	}

	public static Object getSessionAttribute(Object key) {
		return getSession().getAttribute(key);
	}

	public static boolean isLogin() {
		return SecurityUtils.getSubject().getPrincipal() != null;
	}

	public static void logout() {
		SecurityUtils.getSubject().logout();
	}

	public static String getKaptcha(String key) {
		String kaptcha = "";
		if (getSessionAttribute(key) != null) {
			kaptcha = getSessionAttribute(key).toString();
			getSession().removeAttribute(key);
		}
		return kaptcha;
	}
	
	/**
	 * 清空用户的权限缓存
	 */
	public static void clearCachedAuthorizationInfo(){
		RealmSecurityManager securityManager =  (RealmSecurityManager) SecurityUtils.getSecurityManager();
		ShiroAuthorizingRealm userRealm = (ShiroAuthorizingRealm) securityManager.getRealms().iterator().next();
	    userRealm.clearCachedAuthorizationInfo(SecurityUtils.getSubject().getPrincipals());
	}

	/**
	 * 刷新权限
	 */
	public static void refreshAuthorizationInfo() {
		// 重新修改权限后清楚缓存，调用doGetAuthorizationInfo重新取角色的权限信息
		RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
		ShiroAuthorizingRealm shiroRealm = (ShiroAuthorizingRealm) rsm.getRealms().iterator().next();
		Subject subject = SecurityUtils.getSubject();
		String realmName = subject.getPrincipals().getRealmNames().iterator().next();

		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		logger.info("oper.user=" + user.getAccount() + ",login.user="
				+ SecurityUtils.getSubject().getPrincipal().toString());

		// shiroRealm.clearAllCachedAuthorizationInfo2();//清楚所有用户权限
		// 第一个参数为用户名,第二个参数为realmName,test想要操作权限的用户
		SimplePrincipalCollection principals = new SimplePrincipalCollection(user.getAccount(), realmName);
		subject.runAs(principals);
		shiroRealm.getAuthorizationCache().clear();
		shiroRealm.getAuthorizationCache().remove(subject.getPrincipals());
		shiroRealm.getAuthorizationCache().remove(user.getAccount());
		subject.releaseRunAs();
	}

}
